<template>
  <div class="my-container">
    <!-- 顶部头像，昵称 -->
    <van-cell-group v-if="user" class="my-info">
      <van-cell center class="base-info" :border="false">
        <van-image
          class="avatar"
          slot="icon"
          round
          fit="cover"
          src="https://img.yzcdn.cn/vant/cat.jpeg"
        ></van-image>
        <div class="name" slot="title">昵称</div>
        <van-button class="update-btn" size="small" round>编辑资料</van-button>
      </van-cell>
      <van-grid :border="false" class="data-info">
        <van-grid-item class="data-info-item">
          <div slot="text">
            <div class="count">123</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div slot="text">
            <div class="count">123</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div slot="text">
            <div class="count">123</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div slot="text">
            <div class="count">123</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
      </van-grid>
    </van-cell-group>
    <!-- 顶部头像，昵称 -->

    <!-- 如果没有登录显示登录按钮 -->
    <div v-else class="not-login">
      <div class="img" @click="$router.push('/login')"></div>
      <div class="text">登录 / 注册</div>
    </div>

    <!-- 收藏，历史 -->
    <van-grid class="nav-grid mb-4" :column-num="2">
      <van-grid-item
        class="nav-grid-item"
        icon-prefix="iconfont icon"
        icon="shoucang"
        text="收藏"
      >
      </van-grid-item>
      <van-grid-item
        class="nav-grid-item"
        icon-prefix="iconfont icon"
        icon="lishi"
        text="历史"
      >
      </van-grid-item>
    </van-grid>
    <!-- 收藏，历史 -->

    <van-cell title="消息通知" is-link to="/"></van-cell>
    <van-cell class="mb-4" title="小智同学" is-link to="/"></van-cell>
    <van-cell
      v-if="user"
      class="logout-cell"
      title="退出登录"
      @click="onLogout"
    ></van-cell>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getCurrentUser } from "@/api/user.js";

export default {
  name: "MyIndex",
  computed: {
    ...mapState(["user"]),
  },
  created() {
    this.loadCurrentUser();
  },
  methods: {
    onLogout() {
      //提示用户确认
      this.$dialog
        .confirm({
          title: "退出提示",
          message: "确认退出吗？",
        })
        .then(() => {
          this.$store.commit("setUser", null);
        })
        .catch(() => {});
    },
    async loadCurrentUser() {
      const { data } = await getCurrentUser();
      console.log(data);
    },
  },
};
</script>

<style lang='less' scope>
.my-container {
  .my-info {
    background-image: url("./wallhaven-73dvz3.png");
    background-size: cover;
    .base-info {
      box-sizing: border-box;
      height: 115px;
      background-color: unset;
      padding-top: 38px;
      padding-bottom: 11px;
      .avatar {
        width: 66px;
        height: 66px;
        border: 1px solid #fff;
        box-sizing: border-box;
        margin-right: 11px;
      }
      .name {
        color: #fff;
        font-size: 15px;
      }
      .update-btn {
        height: 20px;
        font-size: 10px;
        color: #666;
      }
    }
    .data-info {
      .data-info-item {
        height: 65px;
        color: #fff;
        .count {
          font-size: 18px;
        }
        .text {
          font-size: 11px;
          text-align: center;
        }
      }
    }

    /deep/ .van-grid-item__content {
      background-color: unset;
    }
  }
  .not-login {
    height: 180px;
    background-image: url("./wallhaven-73dvz3.png");
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .img {
      width: 66px;
      height: 66px;
      background-color: #ccc;
    }
    .text {
      color: #fff;
      font-size: 14px;
    }
  }

  /deep/ .nav-grid {
    .nav-grid-item {
      height: 70px;
      .iconfont {
        font-size: 22px;
      }
      .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
      .van-grid-item__text {
        font-size: 14px;
        color: #333;
      }
    }
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 4px;
  }
}
</style>